<?php
namespace JiaLeo\Payment\Unionpay;
use JiaLeo\Payment\Common\PaymentException;
class Notify {

    public $rawData = array();

    /**
     * 回调
     * @return mixed
     * @throws PaymentException
     */
    public function handle($data='',$config) {
//        \Think\Log::record(json_encode($data), 'notify-16', TRUE);
//        \Think\Log::record(json_encode($config['cert_dir']), 'notify-config', TRUE);
        $this->rawData = $data;
        
        $res = \JiaLeo\Payment\Unionpay\Utils\Rsa::verify($data, $config['cert_dir']);
        if (!$res) {
            throw new PaymentException('验证失败!');
        }

        //判断交易状态   判断respCode=00或A6即可认为交易成功
        if (empty($data['respCode']) || !in_array($data['respCode'], ['00', 'A6'])) {
            throw new PaymentException('交易失败!');
        }

        return $data;
    }

    /**
     *  回复成功
     */
    public function returnSuccess() {
        echo 'success';
    }

    /**
     *  回复失败
     */
    public function returnFailure() {
        //echo 'failure';
        throw new PaymentException('failure');
    }

}
